package com.heima.media.controller;

import com.alibaba.excel.EasyExcel;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.media.dto.SensitiveDto;
import com.heima.media.entity.WmSensitive;
import com.heima.media.service.IWmSensitiveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;

/**
 * <p>
 * 敏感词信息表 前端控制器
 * </p>
 *
 * @author SpaceTraveller
 * @since 2022-11-09
 */
@RestController
@RequestMapping("/api/v1/sensitive")
@Api(tags = "敏感词信息表接口")
public class WmSensitiveController{

    @Autowired
    private IWmSensitiveService sensitiveService;

    /**
     * 查询敏感词 sensitives
     * @param dto
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "查询敏感词", notes = "author:SpaceTraveller")
    @ApiImplicitParam(name = "dto", value = "请求体", required = true, dataType = "SensitiveDto")
    public PageResponseResult getSensitiveList(@RequestBody SensitiveDto dto){
        return sensitiveService.getSensitiveList(dto);
    }

    /**
     * 新增敏感词 sensitives
     * @param sensitives
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增敏感词", notes = "author:SpaceTraveller")
    @ApiImplicitParam(name = "sensitives", value = "请求体", required = true, dataType = "Map")
    public ResponseResult createSensitives(@RequestBody Map<String,String> sensitives){
        return sensitiveService.putSensitives(sensitives);
    }

    /**
     * 更新敏感词 sensitives
     * @param sensitives
     * @return
     */
    @PutMapping
    @ApiOperation(value = "更新敏感词", notes = "author:SpaceTraveller")
    @ApiImplicitParam(name = "sensitives", value = "请求体", required = true, dataType = "WmSensitive")
    public ResponseResult updateSensitives(@RequestBody WmSensitive sensitives){
        return sensitiveService.updateSensitives(sensitives);
    }


    /**
     * 更新敏感词 sensitives
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation(value = "通过excel更新敏感词", notes = "author:SpaceTraveller")
    @ApiImplicitParam(name = "file", value = "上传的excel文件", required = true, dataType = "MultipartFile")
    public ResponseResult testExcelPutIn(MultipartFile file) throws IOException {
        return sensitiveService.testExcelPutIn(file);
    }

    /**
     * 文件下载（失败了会返回一个有部分数据的Excel）
     * <p>
     * 1. 创建excel对应的实体对象 参照{@link WmSensitive}
     * <p>
     * 2. 设置返回的 参数
     * <p>
     * 3. 直接写，这里注意，finish的时候会自动关闭OutputStream,当然外面再关闭流问题不大
     */
    @GetMapping("/download")
    public void download(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("敏感词", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), WmSensitive.class).sheet("sheet").doWrite(sensitiveService.list());
    }
}
